#!/bin/bash

#-----------------------------------------------
#BENCHMARKS
#-----------------------------------------------

BENCHMARKS=$HOME'/Benchmarks'
#BENCHMARKS=$MAIN_FOLDER'/Benchmarks'

BENCH_INPUT=$BENCHMARKS'/input'
BENCH_BIN='?'

#bench_list='blackscholes bodytrack swaptions fluidanimate'
#bench_list='barnes fft fmm ocean_ncp radix volrend water_nsquared'
#bench_list='tachyon'

bench_list='blackscholes bodytrack swaptions fluidanimate tachyon barnes fft fmm ocean_ncp radix volrend water_nsquared'

function start_bench {
    $1 $2
}

NUL='/dev/null'

function mv_zero {
    if [[ -e instru0.txt ]]; then
        mv instru0.txt 'instru'$1'.txt'
    fi
}

#-----------------------------------------------
#PARSEC
#-----------------------------------------------

function blackscholes {
    comando=$BENCH_BIN'/blackscholes '$1' '$BENCH_INPUT'/blackscholes/in_16K.txt '$NUL
    #in_16K.txt
    exec_pin _Z9bs_threadPv "$comando"
}

function bodytrack {
    comando=$BENCH_BIN'/bodytrack '$BENCH_INPUT'/bodytrack/sequenceB_1 4 1 1000 5 2 '$1 #output
    #$ sequenceB_2 4 2 2000 5 2 $x
    exec_pin thread_entry "$comando"
}

function swaptions {
    comando=$BENCH_BIN'/swaptions -ns 16 -sm 5000 -nt '$1
    #$ -ns 32 -sm 10000 -nt $x
    exec_pin _Z6workerPv "$comando"
}

function fluidanimate {
    comando=$BENCH_BIN'/fluidanimate '$1' 5 '$BENCH_INPUT'/fluidanimate/in_35K.fluid' #output
    #3 in_15K.fluid
    exec_pin _Z15AdvanceFramesMTPv "$comando"
}

#-----------------------------------------------
#SPLASH
#-----------------------------------------------

function barnes {
    comando=$BENCH_BIN'/barnes'
    cat $BENCH_INPUT/barnes/input.template | sed -E '/NUMPROCS/s//'$1'/' | exec_pin SlaveStart "$comando"
    mv_zero $1
}
function fft {
    #pot de 2
    comando=$BENCH_BIN'/fft -p'$1' -m20 -n131072 -l8'
    exec_pin SlaveStart "$comando"
    mv_zero $1
}
function fmm {
    comando=$BENCH_BIN'/fmm'
    cat $BENCH_INPUT/fmm/input.template | sed -E '/NUMPROCS/s//'$1'/' | exec_pin ParallelExecute "$comando"
    rm times
    mv_zero $1
}
function ocean_cp {
    #pot de 2
    comando=$BENCH_BIN'/ocean_cp -p'$1
    exec_pin slave "$comando"
    mv_zero $1
    #500M
}
function ocean_ncp {
    #pot de 2
    comando=$BENCH_BIN'/ocean_ncp -p'$1
    exec_pin slave "$comando"
    mv_zero $1
    #500M
}
function radix {
    #pot de 2
    comando=$BENCH_BIN'/radix -n4194304 -p'$1
    exec_pin slave_sort "$comando"
    mv_zero $1
    #50M -> 800M
}
function volrend {
    #comando de entrada com bugs
    cp $BENCH_INPUT/volrend/medium/head-scaleddown2.* .
    comando=$BENCH_BIN'/volrend '$1' head-scaleddown2'
    exec_pin Render_Loop "$comando"
    rm head-scaleddown2*
    mv_zero $1
    #200 M
    
}
function water_nsquared {
    #PIN does not find the beginning of the function in thread 0 when it is compiled with clang.
    nt=$(( $1 + 1 ))
    cp $BENCH_INPUT/water_nsquared/small/random.in . 
    comando=$BENCH_BIN'/water_nsquared'
    cat $BENCH_INPUT/water_nsquared/small/input.template | sed -E '/NUMPROCS/s//'$nt'/' | exec_pin WorkStart "$comando"
    rm -f random.in
}

#-----------------------------------------------
#OTHER
#-----------------------------------------------

function tachyon {
    # múltiplos inputs
    entrada='spotlight.dat'
    nt=$(( $1 + 1 ))
    comando=$BENCH_BIN'/tachyon '$BENCH_INPUT'/tachyon/scenes/'$entrada' -numthreads '$nt
    exec_pin thread_slave "$comando"
    rm outfile.tga
}

